package com.atguigu.process.service.impl;

import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.model.process.ProcessType;
import com.atguigu.process.mapper.OaProcessTypeMapper;
import com.atguigu.process.service.OaProcessTemplateService;
import com.atguigu.process.service.OaProcessTypeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 审批类型 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-04-20
 */
@Service
public class OaProcessTypeServiceImpl extends ServiceImpl<OaProcessTypeMapper, ProcessType> implements OaProcessTypeService {

    @Autowired
    private OaProcessTemplateService processTemplateService;

    /**
     * 查询所有审批分类和每个分类所有审批模板
     *
     * @return {@link List <ProcessType>}
     */
    @Override
    public List<ProcessType> findProcessType() {
        // 1.查询所有审批分类，返回 list 集合
        List<ProcessType> processTypeList = list();
        // 2.遍历返回所有审批分类 list 集合
        return processTypeList.stream().peek(item -> {
            // 3.得到每个审批分类，根据审批分类 id 查询对应审批模板
            Long typeId = item.getId();
            LambdaQueryWrapper<ProcessTemplate> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProcessTemplate::getProcessTypeId, typeId);
            List<ProcessTemplate> processTemplateList = processTemplateService.list(wrapper);
            // 4.根据审批分类 id 查询对应审批模板数据 (List) 封装到每个审批分类对象里面
            item.setProcessTemplateList(processTemplateList);
        }).collect(Collectors.toList());
    }
}
